﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TreeGraphicalControl;
using FeatureModelTool.ProjectView.ProjectTreeStructures;
using Collections.TreeCollections;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using System.Windows.Media;
using FeatureModelTool.Windows;
using FeatureModelCore;
using FeatureModelTool.ProjectView.MenuItems;
using FeatureModelTool.FileView;

namespace FeatureModelTool.ProjectView.TreeItems
{
    public class OpenFilePair
    {
        public FileLoaderType DefualtOpenType { get; set; }
        public string FileExtension { get; protected set; }
        public OpenFilePair(string fileEx)
        {
            FileExtension = fileEx;
        }
    }
    public class TreeItem_ProjectFile : TreeViewItemControl
    {
        private static List<OpenFilePair> _defaultOpenList = new List<OpenFilePair>();

        static TreeItem_ProjectFile()
        {
            OpenFilePair fmFile = new OpenFilePair("fm");
            fmFile.DefualtOpenType = FileLoaderType.FeatureGraphicalView;
            OpenFilePair configFile = new OpenFilePair("config");
            configFile.DefualtOpenType = FileLoaderType.ConfigTreeView;
            _defaultOpenList.Add(fmFile);
            _defaultOpenList.Add(configFile);
        }

        #region Properties
        public ProjectFile ProjectTreeInstance { get; protected set; }

        #endregion

        private readonly static string _ICON_SOURCE = "/FeatureModelTool;component/Images/feature.png";


        public TreeItem_ProjectFile(ProjectFile file)
            : base()
        {
            ProjectTreeInstance = file;

            #region Header
            this.Header = MainWindow.GenerateTreeItemHeader(file.Name, _ICON_SOURCE
                , Brushes.White);
            #endregion

            //MouseButtonEventHandler
            this.MouseDoubleClick += new MouseButtonEventHandler(this.mouseDouble_Click);
        }


        /// <summary>
        /// Tree Item Construct
        /// </summary>
        /// <param name="treeObj"></param>
        /// <returns></returns>
        public static TreeViewItemControl ConstructTreeItem(TreeObject treeObj)
        {
            ProjectFile projFile = treeObj as ProjectFile;
            if (projFile == null)
            {
                return null;
            }
            return new TreeItem_ProjectFile(projFile);
        }

        #region Context Menu
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tree"></param>
        /// <returns></returns>
        public static ContextMenu ContextGenerator(TreeViewItemControl treeItem, TreeObject treeObj)
        {
            ProjectFile file = treeObj as ProjectFile;
            TreeItem_ProjectFile treeItem_File = treeItem as TreeItem_ProjectFile;
            if(file == null)
            {
                return null;
            }
           
            // open file 
            MenuItem_File open = new MenuItem_File(treeItem_File, (ProjectTree)file.TreeOwner, file)
            {
                Name = file.ID.ToString(),
                Header = "Open"
            };
            // open with plain text
            MenuItem_File openWithPlainText = new MenuItem_File(treeItem_File, (ProjectTree)file.TreeOwner, file)
            {
                Name = file.ID.ToString(),
                Header = "Open With Plain Text"
            };
            openWithPlainText.Click += new RoutedEventHandler(TreeItem_ProjectFile.openWithPlainText);
            // open with plain text
            MenuItem_File openWithTreeView = new MenuItem_File(treeItem_File, (ProjectTree)file.TreeOwner, file)
            {
                Name = file.ID.ToString(),
                Header = "Open With Tree View"
            };
            openWithTreeView.Click += new RoutedEventHandler(TreeItem_ProjectFile.openWithTreeView);
            // open with plain text
            MenuItem_File openWithGraphic = new MenuItem_File(treeItem_File, (ProjectTree)file.TreeOwner, file)
            {
                Name = file.ID.ToString(),
                Header = "Open With Graphical"
            };
            // open with Config
            MenuItem_File openWithConfig = new MenuItem_File(treeItem_File, (ProjectTree)file.TreeOwner, file)
            {
                Name = file.ID.ToString(),
                Header = "Open With Config View"
            };
            openWithConfig.Click += new RoutedEventHandler(TreeItem_ProjectFile.openWithConfig);


            open.Items.Add(openWithPlainText);
            open.Items.Add(openWithTreeView);
            open.Items.Add(openWithGraphic);
            open.Items.Add(openWithConfig);
            
            // rename
            MenuItem_File rename = new MenuItem_File(treeItem_File, (ProjectTree)file.TreeOwner, file)
            {
                Name = file.ID.ToString(),
                Header = "Rename"
            };
            rename.Click += new RoutedEventHandler(TreeItem_ProjectTreeRoot.Rename_EventHandler);

            ContextMenu contextMenu = new ContextMenu();
            contextMenu.Items.Add(open);
            contextMenu.Items.Add(rename);
            return contextMenu;
        }
        #endregion

        #region Event Handler
        /// <summary>
        /// Open with plain text
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected static void openWithPlainText(object sender, RoutedEventArgs e)
        {
            try
            {
                MenuItem_File file = sender as MenuItem_File;
                if (file == null)
                {
                    return;
                }
                setFileLoaderType(file.File.FileName, FileLoaderType.Text);
                openFile(file.TreeItem, FileLoaderType.Text);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }
        /// <summary>
        /// Open with plain text
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected static void openWithTreeView(object sender, RoutedEventArgs e)
        {
            try
            {

                MenuItem_File file = sender as MenuItem_File;
                if (file == null)
                {
                    return;
                }
                setFileLoaderType(file.File.FileName, FileLoaderType.FeatureTreeView);
                openFile(file.TreeItem, FileLoaderType.FeatureTreeView);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }
        /// <summary>
        /// Open with plain text
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected static void openWithGraphic(object sender, RoutedEventArgs e)
        {
            try
            {
                MenuItem_File file = sender as MenuItem_File;
                if (file == null)
                {
                    return;
                }
                setFileLoaderType(file.File.FileName, FileLoaderType.FeatureGraphicalView);
                openFile(file.TreeItem, FileLoaderType.FeatureGraphicalView);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }
        /// <summary>
        /// Open with Config
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected static void openWithConfig(object sender, RoutedEventArgs e)
        {
            try
            {
                MenuItem_File file = sender as MenuItem_File;
                if (file == null)
                {
                    return;
                }
                setFileLoaderType(file.File.FileName, FileLoaderType.ConfigTreeView);
                openFile(file.TreeItem, FileLoaderType.ConfigTreeView);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }

        /// <summary>
        /// Double click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void mouseDouble_Click(object sender, MouseButtonEventArgs e)
        {
            try
            {
                TreeItem_ProjectFile file = sender as TreeItem_ProjectFile;
                if (file == null)
                {
                    return;
                }

                FileLoaderType loaderType = getFileLoaderType(file.ProjectTreeInstance.FileName);
                openFile(file, loaderType);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message + exp.StackTrace);
            }
        }
        /// <summary>
        /// Open file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="type"></param>
        protected static void openFile(TreeItem_ProjectFile file, FileLoaderType type)
        {
            string fileFullPath = file.ProjectTreeInstance.FullFilePath;
            // create new instance in tab
            MainWindow windows = MainWindow.GetCurrentWindowsInstance(file);
            windows.FileViewer.ConstructNewTab(fileFullPath, type);
        }
        #endregion

        #region File Loader Type
        /// <summary>
        /// Get File Loader Type
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private static FileLoaderType getFileLoaderType(string fileName)
        {
            string[] words = fileName.Split('.');
            if (words.Length == 1)
            {
                return FileLoaderType.Text;
            }
            string ex = words[words.Length - 1];
            foreach (OpenFilePair file in _defaultOpenList)
            {
                if (file.FileExtension.ToLower() == ex.ToLower())
                {
                    return file.DefualtOpenType;
                }
            }
            return FileLoaderType.Text;
        }

        private static void setFileLoaderType(string fileName, FileLoaderType type)
        {
            string[] words = fileName.Split('.');
            if (words.Length == 1)
            {
                return;
            }
            string ex = words[words.Length - 1];
            foreach (OpenFilePair file in _defaultOpenList)
            {
                if (file.FileExtension.ToLower() == ex.ToLower())
                {
                    file.DefualtOpenType = type;
                }
            }
        }
        #endregion

    }
}
